package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Role;
import com.af.monitorModule.generator.entity.User;
import com.af.monitorModule.generator.mapper.RoleMapper;
import com.af.monitorModule.generator.service.RoleService;
import com.af.monitorModule.response.exception.CustomerException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-02-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public JSONObject getRoles(String page,String limit) {
        if (page == null || limit == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        Page<Role> p = new Page<Role>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        roleMapper.selectPage(p, wrapper);
        JSONObject res = new JSONObject();
        res.put("data",p.getRecords());
        res.put("code",0);
        res.put("count",this.count(wrapper));
        res.put("msg","角色列表");
        return res;
    }

    @Override
    public JSONObject changeState(String roleId) {
        Integer id = Integer.valueOf(roleId);
        Role change = roleMapper.selectById(id);
        if (change == null){
            throw new CustomerException(ResultEnum.ROLE_ISNOT_EXIST);
        }
        change.setState(change.getState() == 1 ? 0:1);
        roleMapper.updateById(change);
        JSONObject res = new JSONObject();
        res.put("code",200);
        return res;
    }
}
